package com.scnu.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scnu.edu.entity.MovieCategoryRelation;
import com.scnu.edu.mapper.MovieCategoryRelationMapper;
import com.scnu.edu.service.MovieCategoryRelationService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MovieCategoryRelationServiceImpl extends ServiceImpl<MovieCategoryRelationMapper, MovieCategoryRelation> implements MovieCategoryRelationService {

    @Override
    public boolean saveBatch(Long movieId, List<Long> categoryIds) {
        List<MovieCategoryRelation> relations = categoryIds.stream()
                .map(categoryId -> {
                    MovieCategoryRelation relation = new MovieCategoryRelation();
                    relation.setMovieId(movieId);
                    relation.setCategoryId(categoryId);
                    return relation;
                })
                .collect(Collectors.toList());
        return saveBatch(relations);
    }

    @Override
    public List<Long> getCategoryIdsByMovieId(Long movieId) {
        QueryWrapper<MovieCategoryRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("movie_id", movieId);
        return list(wrapper).stream()
                .map(MovieCategoryRelation::getCategoryId)
                .collect(Collectors.toList());
    }

    @Override
    public List<Long> getMovieIdsByCategoryId(Long categoryId) {
        QueryWrapper<MovieCategoryRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);
        return list(wrapper).stream()
                .map(MovieCategoryRelation::getMovieId)
                .collect(Collectors.toList());
    }

    @Override
    public boolean removeByMovieId(Long movieId) {
        QueryWrapper<MovieCategoryRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("movie_id", movieId);
        return remove(wrapper);
    }
}
